package gui.data.D2;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.TriangleCalculator;

@SuppressWarnings("serial")
public class TriangleInputGui extends JPanel implements ActionListener {
	JTextField abInput, bcInput, caInput, abHeightInput, bcHeightInput, caHeightInput, cabAngleInput,
			abcAngleInput, bcaAngleInput, circumfInput, areaInput;
	MyFrame myFrame;
	JLabel abLbl, bcLbl, caLbl, abHeightLbl, bcHeightLbl, caHeightLbl, cabAngleLbl, abcAngleLbl,
			bcaAngleLbl, circumfLbl, areaLbl;
	int selection = 3;

	public TriangleInputGui(final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(0, 2));
		this.setPreferredSize(new Dimension(500, 500));

		ButtonGroup group = new ButtonGroup();

		JRadioButton equilateralTriangle = new JRadioButton("Equilateral");
		equilateralTriangle.setActionCommand("Equilateral");
		equilateralTriangle.addActionListener(this);
		// vordhaarne
		JRadioButton isoscelesTriangle = new JRadioButton("Isosceles");
		isoscelesTriangle.setActionCommand("Isosceles");
		isoscelesTriangle.addActionListener(this);

		JRadioButton scaleneTriangle = new JRadioButton("Scalene");
		scaleneTriangle.setActionCommand("Scalene");
		scaleneTriangle.addActionListener(this);
		
		JRadioButton rightTriangle = new JRadioButton("Right");
		rightTriangle.setActionCommand("Right");
		rightTriangle.addActionListener(this);

		group.add(equilateralTriangle);
		group.add(isoscelesTriangle);
		group.add(scaleneTriangle);
		group.add(rightTriangle);

		abLbl = new JLabel("Length of |AB|");
		bcLbl = new JLabel("Length of |BC|");
		caLbl = new JLabel("Length of |CA|");
		abHeightLbl = new JLabel("Height from base AB");
		bcHeightLbl = new JLabel("Height from base BC");
		caHeightLbl = new JLabel("Height from base CA");
		cabAngleLbl = new JLabel("Angle " + "\u2220CAB");
		abcAngleLbl = new JLabel("Angle " + "\u2220ABC");
		bcaAngleLbl = new JLabel("Angle " + "\u2220BCA");
		circumfLbl = new JLabel("Circumference");
		areaLbl = new JLabel("Area ");

		abInput = new JTextField();
		bcInput = new JTextField();
		caInput = new JTextField();
		abHeightInput = new JTextField();
		bcHeightInput = new JTextField();
		caHeightInput = new JTextField();
		cabAngleInput = new JTextField();
		abcAngleInput = new JTextField();
		bcaAngleInput = new JTextField();
		circumfInput = new JTextField();
		areaInput = new JTextField();

		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});

		this.add(equilateralTriangle);
		this.add(isoscelesTriangle);
		this.add(scaleneTriangle);
		this.add(rightTriangle);
		this.add(abLbl);
		this.add(abInput);
		this.add(bcLbl);
		this.add(bcInput);
		this.add(caLbl);
		this.add(caInput);
		this.add(abHeightLbl);
		this.add(abHeightInput);
		this.add(bcHeightLbl);
		this.add(bcHeightInput);
		this.add(caHeightLbl);
		this.add(caHeightInput);
		this.add(cabAngleLbl);
		this.add(cabAngleInput);
		this.add(abcAngleLbl);
		this.add(abcAngleInput);
		this.add(bcaAngleLbl);
		this.add(bcaAngleInput);
		this.add(circumfLbl);
		this.add(circumfInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(new JLabel(""));
		this.add(calculate);

		scaleneTriangle.setSelected(true);
		isoscelesTriangle.setEnabled(true);
		equilateralTriangle.setEnabled(true);
		rightTriangle.setEnabled(true);
	}

	@SuppressWarnings("unused")
	private void openResult() {
		boolean error = false;
		JTextField[] textFields = {abInput, bcInput, caInput, abHeightInput, bcHeightInput, caHeightInput, cabAngleInput,
				abcAngleInput, bcaAngleInput, circumfInput, areaInput};
		Double[] values = new Double[11];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new TriangleCalculator(selection, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], myFrame);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Equilateral")) {
			selection = 1;
			abInput.setEnabled(true);
			bcInput.setEnabled(false);
			caInput.setEnabled(false);
			abHeightInput.setEnabled(true);
			bcHeightInput.setEnabled(false);
			caHeightInput.setEnabled(false);
			cabAngleInput.setEnabled(false);
			abcAngleInput.setEnabled(false);
			bcaAngleInput.setEnabled(false);
			circumfInput.setEnabled(true);
			areaInput.setEnabled(true);
		} else if (e.getActionCommand().equals("Isosceles")) {
			selection = 2;
			abInput.setEnabled(true);
			bcInput.setEnabled(true);
			caInput.setEnabled(false);
			abHeightInput.setEnabled(true);
			bcHeightInput.setEnabled(true);
			caHeightInput.setEnabled(false);
			cabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(false);
			bcaAngleInput.setEnabled(true);
			circumfInput.setEnabled(true);
			areaInput.setEnabled(true);
		} else if (e.getActionCommand().equals("Scalene")) {
			selection = 3;
			abInput.setEnabled(true);
			bcInput.setEnabled(true);
			caInput.setEnabled(true);
			abHeightInput.setEnabled(true);
			bcHeightInput.setEnabled(true);
			caHeightInput.setEnabled(true);
			cabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcaAngleInput.setEnabled(true);
			circumfInput.setEnabled(true);
			areaInput.setEnabled(true);
		} else if (e.getActionCommand().equals("Right")) {
			selection = 4;
			abInput.setEnabled(true);
			bcInput.setEnabled(true);
			caInput.setEnabled(true);
			abHeightInput.setEnabled(true);
			bcHeightInput.setEnabled(false);
			caHeightInput.setEnabled(false);
			cabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcaAngleInput.setEnabled(false);
			circumfInput.setEnabled(true);
			areaInput.setEnabled(true);
		}
	}
}
